package com.pengxun.manager.controller.system;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pengxun.manager.model.system.Role;
import com.pengxun.manager.model.system.Userinfo;
import com.pengxun.manager.service.system.LoginService;
import com.pengxun.manager.service.system.RoleMenuService;
import com.pengxun.manager.service.system.RoleService;
import com.pengxun.manager.vo.RoleParam;
import com.pengxun.manager.vo.RolePermissionVO;
import com.szeastroc.common.exception.EastrocException;
import com.szeastroc.common.jdbc.page.Page;
import com.szeastroc.common.result.Result;
import com.szeastroc.common.result.ResultUtils;

@RestController
@RequestMapping("/role")
public class RoleController {

	@Resource
	private RoleService roleService;
	
	@Resource
	private LoginService loginService;
	
	@Resource
	private RoleMenuService roleMenuService;

	@RequestMapping("/insert")
	public Result insert(@RequestBody @Validated Role role) {
		try {
			Userinfo userinfo = loginService.getCurrentUser();
			role.setCreateBy(userinfo.getUserId());
			role.setCreateDate(new Date());
			roleService.insert(role);
		}catch(DuplicateKeyException e){
			return ResultUtils.renderError("角色code重复");
		}catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
		}
		return ResultUtils.render();
	};

	@RequestMapping("/update")
	public Result update(@RequestBody Role role) {
		try {
			roleService.update(role);
		} catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
		}
		return ResultUtils.render();
	};

	@RequestMapping("/delete")
	public Result delete(Integer id) {
		try {
			roleService.delete(id);
		} catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
		}
		return ResultUtils.render();
	};
	
	@RequestMapping("/deleteByRoleCode")
	public Result deleteByRoleCode(String roleCode) {
		try {
			roleService.deleteByRoleCode(roleCode);
		}catch (EastrocException e) {
			return ResultUtils.renderError(e.getMessage());
		}catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
		}
		return ResultUtils.render();
	};
	
	@RequestMapping("/findAll")
	public Result findByPage() {
		try{
			List<Role> list = roleService.findAll();
			return ResultUtils.render(list);
		}catch(Exception e){
			return ResultUtils.renderError("查询异常");
		}
	};

	@RequestMapping("/findByPage")
	public Result findByPage(@RequestBody RoleParam role) {
		Page page = roleService.findByPage(role);
		return ResultUtils.render(page);
	};
	

	@RequestMapping("/findByRoleCode")
	public Result findByRoleCode(String roleCode) {
		try {
			Role role = roleService.findByRoleCode(roleCode);
			return ResultUtils.render(role);
		}catch (EastrocException e) {
			return ResultUtils.renderError(e.getMessage());
		}catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
		}
	};
	
	/**
	 * 启用禁用 角色
	 * @param role
	 * @return
	 */
	@RequestMapping("/updateStatus")
	public Result updateStatus(String roleCode,Integer status) {
		
		try {
			if(StringUtils.isBlank(roleCode) || null == status){
				return ResultUtils.renderError("参数错误");
			}
			Role role = new Role();
			role.setRoleCode(roleCode);
			role.setStatus(status);
			roleService.update(role);
		} catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
		}
		return ResultUtils.render();
	};
	
	@RequestMapping("/getRolePermission")
	public Result getRolePermission(String roleCode){
		try {
			RolePermissionVO rolePermission = roleMenuService.getRolePermission(roleCode);
			return ResultUtils.render(rolePermission);
		} catch (Exception e) {
			return ResultUtils.renderError(e.getMessage());
		}
	}
	
	@RequestMapping("/updateRolePermission")
	public Result updateRolePermission(@RequestBody RolePermissionVO rolePermission){
		try {
			roleMenuService.updateRolePermission(rolePermission);
			return ResultUtils.render();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtils.renderError(e.getMessage());
		}
	}
	
	@RequestMapping("/findRoleByCurrentUser")
	public Result findRoleByCurrentUser() {
		try{
			Userinfo userinfo = loginService.getCurrentUser();
			List<Role> list = roleService.findRoleByCurrentUser(userinfo);
			return ResultUtils.render(list);
		}catch(Exception e){
			return ResultUtils.renderError("查询异常");
		}
	};
	
}
